'abutilemap.monkey by Belimoth

'TODO

Strict



Class TileMapBase<T>
	Field cellWidth:Int, cellHeight:Int
	Field width:Int, height:Int
	Field data:T[][]
	Field edgeValue:T
	
	Method Initialize:Void(cellWidth:Int, cellHeight:Int, data:T[][])
		Self.cellWidth = cellWidth
		Self.cellHeight = cellHeight
		Self.width = data.Length
		Self.height = data[0].Length
		Self.data = data
	End
	
	Method Initialize:Void(cellWidth:Int, cellHeight:Int, width:Int, height:Int)
		Self.cellWidth = cellWidth
		Self.cellHeight = cellHeight
		Self.width = width
		Self.height = height
		data = data.Resize(width)
		For Local x := 0 Until width
			data[x] = data[x].Resize(height)
		Next
	End
	
	Method Get:T(x:Int, y:Int)
		If (x >= 0) And (x < width) And (y >= 0) And (y < height)
			Return data[x][y]
		Endif
		Return edgeValue
	End
	
	Method Set:Void(x:Int, y:Int, value:T)
		If (x >= 0) And (x < width) And (y >= 0) And (y < height)
			data[x][y] = value
		Endif
	End
	
	Method MapX:Int(x:Float)
		Return Floor( x / Float(cellWidth) )
	End
	
	Method MapY:Int(y:Float)
		Return Floor( y / Float(cellHeight) )
	End
	
	Method At:T(x:Float, y:Float)
		Return Get( MapX(x), MapY(y) )
	End
End